<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   zhaozilong
 * @Last Modified time: 2018-12-26 16:17:46
 */

namespace app\common\model;

class Config extends BaseNew {
	// 设置数据表（不含前缀）
	//protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	public function getConfigsBySpace($config_space) {
		$data = $this->where(array('config_space'=>$config_space))->select();
		$data = $data->toArray(); //转换为数组
		$list = array();
		foreach ($data as $key => $value) {
			$list[$value['config_name']] = $value['config_value'];
		}
		if ($config_space == 'global') {
			//支付方式
			$list['is_alipay'] = $list['allow_pay_method'] & 1 ? 1 : 0;
			$list['is_wxpay'] = $list['allow_pay_method'] & 2 ? 1 : 0;
			
			//客服二维码
			$ftpWeb = config('app.ftp_web');
			$list['kefu_ewm'] = $list['kefu_ewm'] ? $ftpWeb.$list['kefu_ewm'] : '';
		}
		return $list;
	}

	public function getConfigByNameAndSpace($config_name, $config_space) {
		$info = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->find();
		return $info;
	}

	public function getConfigValueByNameAndSpace($config_name, $config_space) {
		$info = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->find();
		return $info['config_value'];
	}

	public function updateConfigByNameAndSpace($config_name, $config_space, $data) {
		$result = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->data($data)->update();
		return $result;
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit) {
		$data = $this->where($map)->order('create_time desc')->limit($limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');

		$list = array();
		foreach ($data as $key => $value) {
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//$value['create_time'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';

			$list[$value['id']] = $value;
		}
		return array($list);
	}

}